package com.mmall.product.provider.controller;

import com.common.framework.bo.PageResultBO;
import com.common.framework.dto.PageDTO;
import com.mmall.common.spring.security.LoginRequired;
import com.mmall.common.spring.security.UserSecurityContextHolder;
import com.mmall.product.api.bo.ProductBO;
import com.mmall.product.api.bo.ProductDetailBO;
import com.mmall.product.api.dto.ProductDetailDTO;
import com.mmall.product.api.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author jianghucoding
 * @date 2019/10/17
 */
@RestController
@RequestMapping("/product")
@Validated
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    public PageResultBO<ProductBO> products(@Valid PageDTO page) {
        return productService.findProducts(page);
    }

    @GetMapping("/detail")
    public ProductDetailBO detail(@NotNull Long id) {
        return productService.detail(id);
    }

    @PostMapping("/create")
    @LoginRequired
    public void createProduct(@Valid ProductDetailDTO productDetailDTO) {
        Long userId = UserSecurityContextHolder.getContext().getUserId();
        productService.createProduct(userId, productDetailDTO);
    }
}
